package com.robot.web;

import giovynet.nativelink.SerialPort;
import giovynet.serial.Baud;
import giovynet.serial.Com;
import giovynet.serial.Parameters;

import java.util.List;

public class EnvioSerial {

	void transmision(String bin) throws Exception {

		SerialPort serialPort = new SerialPort();
		byte[] buffer = new byte[1024];
		char send[] = bin.toCharArray();
		int len = send.length - 1;
		List<String> lstFreeSerialPort = serialPort.getFreeSerialPort();
		if (lstFreeSerialPort.size() > 0) {

			Parameters parameters = new Parameters();
			parameters.setPort(lstFreeSerialPort.get(0));
			parameters.setBaudRate(Baud._9600);
			parameters.setByteSize("8");
			parameters.setParity("N");
			Com com = new Com(parameters);
			System.out.print("puerto: " + lstFreeSerialPort.get(0) + "\n");
			System.out.print("Enviado: " + bin + "\n");
			System.out.print("Recibido: ");
			for (int i = 0; i < len; i++) {
				com.sendArrayChar(send);
				Thread.sleep(0);
				char dataReceived = com.receiveSingleChar();
				System.out.print(dataReceived);
				Thread.sleep(0);
			}
			System.out.println("\n -- End of transmission--");
			com.close();
		} else {
			System.out.println("There is no free serial ports.");
		}
	}

}
